﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using TruckingApplication.Dal.EntityModels;

namespace TruckingApplication.Dal
{
    public class TruckingDBInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
    {
        protected override void Seed(EntitiesContext context)
        {
            base.Seed(context);

            // Trailer Types
            var trailerTypes = new List<TRAILER_TYPE>
            {
                new TRAILER_TYPE { type = "Flat Bed"},
                new TRAILER_TYPE { type = "Refer"}
            };
            trailerTypes.ForEach(t => context.TrailerTypes.Add(t));
            context.SaveChanges();

            // Company Types
            var companyTypes = new List<COMPANY_TYPE>
            {
                new COMPANY_TYPE { companyType = "Shipper", descrip = "You have products to ship." },
                new COMPANY_TYPE { companyType = "Broker", descrip = "You have loads to manage." },
                new COMPANY_TYPE { companyType = "Transportation", descrip = "You have trucks to carry loads." }
            };
            companyTypes.ForEach(c => context.CompanyTypes.Add(c));
            context.SaveChanges();

            // Resource Types
            var resourceTypes = new List<RESOURCE_TYPE>
            {
                new RESOURCE_TYPE { resourceType = "ADMINISTRATOR" },
                new RESOURCE_TYPE { resourceType = "DISPATCHER" },
                new RESOURCE_TYPE { resourceType = "CARRIER" },
                new RESOURCE_TYPE { resourceType = "BROKER" }
            };
            resourceTypes.ForEach(r => context.ResourceTypes.Add(r));
            context.SaveChanges();

            // Resource
            var resources = new List<RESOURCE>
            {
                new RESOURCE
                {
                    firstName = "Robert", lastName = "Green", address = "123 Main St.", city = "Miami", state = "FL", zip = 33054, startDate = DateTime.Now, ssNum = "111-11-1111", email = "r@green.com", phone = "111-111-1111", commission = 0, resourceTypeID = resourceTypes[0].resourceTypeID, mcNumber = "123-ABC-123", factoringFee = 50, isActive = true, dob = null
                },
                new RESOURCE
                {
                    firstName = "James", lastName = "Green", address = "123 Main St.", city = "Miami", state = "FL", zip = 33054, startDate = DateTime.Now, ssNum = "211-11-1111", email = "j@green.com", phone = "222-111-1111", commission = 0, resourceTypeID = resourceTypes[1].resourceTypeID, mcNumber = "124-ABC-123", factoringFee = 50, isActive = true, dob = null
                },
                new RESOURCE
                {
                    firstName = "Veronica", lastName = "Green", address = "123 Main St.", city = "Miami", state = "FL", zip = 33054, startDate = DateTime.Now, ssNum = "311-11-1111", email = "v@green.com", phone = "333-111-1111", commission = 0, resourceTypeID = resourceTypes[2].resourceTypeID, mcNumber = "124-ABC-123", factoringFee = 50, isActive = true, dob = null
                },
                new RESOURCE
                {
                    firstName = "Sandra", lastName = "Green", address = "123 Main St.", city = "Miami", state = "FL", zip = 33054, startDate = DateTime.Now, ssNum = "411-11-1111", email = "s@green.com", phone = "444-111-1111", commission = 0, resourceTypeID = resourceTypes[3].resourceTypeID, mcNumber = "124-ABC-123", factoringFee = 50, isActive = true, dob = null
                }
            };
            resources.ForEach(r => context.Resources.Add(r));
            context.SaveChanges();

            // Company
            var company = new List<COMPANY>
            {
                new COMPANY{ companyName = "HP Partners", adminResourceID = resourceTypes[0].resourceTypeID, address = "123 My Street", city = "Hollywood", state = "FL", zip = 33020, companyTypeID = companyTypes[0].companyTypeID, taxID = "11-00000000", email = "m@biz.com", phone = "954-999-9999", web = "www.Edu.com" },
                new COMPANY{ companyName = "Pointsmith", adminResourceID = resourceTypes[0].resourceTypeID, address = "123 My Street", city = "Katy", state = "TX", zip = 77494, companyTypeID = companyTypes[0].companyTypeID, taxID = "11-00000000", email = "m@ps.com", phone = "501-999-9999", web = "www.ps.com" }
            };
            company.ForEach(c => context.Companies.Add(c));
            context.SaveChanges();

            // Company Resource
            var companyResource = new List<RESOURCE_COMPANY>
            {
                new RESOURCE_COMPANY { resourceID = resources[0].resourceID, companyID = company[0].companyID },
                new RESOURCE_COMPANY { resourceID = resources[1].resourceID, companyID = company[1].companyID }
            };
            companyResource.ForEach(cr => context.ResourceCompanies.Add(cr));
            context.SaveChanges();

            // Trailer
            var trailers = new List<TRAILER>
            {
                new TRAILER { resourceID = resources[0].resourceID, vin = "11111111111111111", mileage = 10000, trailerTypeID = trailerTypes[0].trailerTypeID},
                new TRAILER { resourceID = resources[0].resourceID, vin = "22222222222222222", mileage = 20000, trailerTypeID = trailerTypes[1].trailerTypeID}
            };
            trailers.ForEach(t => context.Trailers.Add(t));
            context.SaveChanges();

            // Insurance
            var insurance = new List<INSURANCE>
            {
                new INSURANCE { paymentAmount = 50, paymentTerms ="monthly", startDate = DateTime.Now, endDate = DateTime.Now.AddMonths(12), total = 850.00M, trailerID = trailers[0].trailerID},
                new INSURANCE { paymentAmount = 150, paymentTerms ="monthly", startDate = DateTime.Now, endDate = DateTime.Now.AddMonths(6), total = 350.00M, trailerID = trailers[1].trailerID}
            };
            insurance.ForEach(i => context.Insurances.Add(i));
            context.SaveChanges();

            // Roadside Assistance
            var roadsideAsst = new List<ROADSIDE_ASSISTANCE>
            {
                new ROADSIDE_ASSISTANCE { paymentAmount = 50, paymentTerms ="monthly", startDate = DateTime.Now, endDate = DateTime.Now.AddMonths(12), total = 850.00M, trailerID = trailers[0].trailerID},
                new ROADSIDE_ASSISTANCE { paymentAmount = 250, paymentTerms ="monthly", startDate = DateTime.Now, endDate = DateTime.Now.AddMonths(12), total = 850.00M, trailerID = trailers[0].trailerID},
            };
            roadsideAsst.ForEach(r => context.ResourceAssistances.Add(r));
            context.SaveChanges();

            // Log
            var logs = new List<LOG>
            {
                new LOG { logDate = DateTime.Now, previousMileage = 0, currentMileage = 1000, variance = 1000 },
                new LOG { logDate = DateTime.Now.AddDays(3), previousMileage = 1000, currentMileage = 1500, variance = 500 }
            };
            logs.ForEach(l => context.Logs.Add(l));
            context.SaveChanges();

            // Tailer Log
            var trailerLogs = new List<TRAILER_LOG>
            {
                new TRAILER_LOG { trailerID = trailers[0].trailerID, logID = logs[0].logID },
                new TRAILER_LOG { trailerID = trailers[1].trailerID, logID = logs[1].logID }
            };
            trailerLogs.ForEach(tl => context.TrailerLogs.Add(tl));
            context.SaveChanges();

            // Payment
            var payments = new List<PAYMENT>
            {
                new PAYMENT { resourceID = resources[0].resourceID, paymentDate = DateTime.Now, paymentAmount = 100, paymentType = "Payable" },
                new PAYMENT { resourceID = resources[1].resourceID, paymentDate = DateTime.Now, paymentAmount = 75, paymentType = "Account Receivable" }
            };
            payments.ForEach(p => context.Payments.Add(p));
            context.SaveChanges();

            // Resource_Payment
            var resourcePayments = new List<RESOURCE_PAYMENT>
            {
                new RESOURCE_PAYMENT { resourceID = resources[0].resourceID, paymentID = payments[0].paymentID},
                new RESOURCE_PAYMENT { resourceID = resources[1].resourceID, paymentID = payments[1].paymentID}
            };
            resourcePayments.ForEach(r => context.ResourcePayments.Add(r));
            context.SaveChanges();

            // Order Status
            var orderStatuses = new List<ORDER_STATUS>
            {
                new ORDER_STATUS { status = "Delivered" },
                new ORDER_STATUS { status = "In Route" },
                new ORDER_STATUS { status = "Cancelled" }
            };
            orderStatuses.ForEach(o => context.OrderStatuses.Add(o));
            context.SaveChanges();

            // Order
            var orders = new List<ORDER>
            {
                new ORDER { orderNum = 123, dispatcherID = resources[1].resourceID, gross = 1000, net = 500, carrierRate = 350, max = 375, paidStatus = false, dueDate = DateTime.Now.AddDays(2), mileage = 425, deadHeadMileage = 0, quickPay = false, lumperFee = 0, brokerID = resources[3].resourceID, carrierID = resources[2].resourceID, pickupCity = "Miami", pickupState = "FL", dropOffCity = "Orlando", dropOffState = "FL", statusID = orderStatuses[0].oStatusID, notes = "n/a", resourceID = resources[0].resourceID, active = true },
                new ORDER { orderNum = 125, dispatcherID = resources[1].resourceID, gross = 1000, net = 500, carrierRate = 350, max = 375, paidStatus = false, dueDate = DateTime.Now.AddDays(2), mileage = 425, deadHeadMileage = 0, quickPay = false, lumperFee = 0, brokerID = resources[3].resourceID, carrierID = resources[2].resourceID, pickupCity = "Miami", pickupState = "FL", dropOffCity = "Orlando", dropOffState = "FL", statusID = orderStatuses[0].oStatusID, notes = "n/a", resourceID = resources[0].resourceID, active = true }
            };
            orders.ForEach(o => context.Orders.Add(o));
            context.SaveChanges();

            // Order Resource
            var orderResources = new List<ORDER_RESOURCE>
            {
                new ORDER_RESOURCE { orderID = orders[0].orderID, resourceID = resources[0].resourceID },
                new ORDER_RESOURCE { orderID = orders[1].orderID, resourceID = resources[1].resourceID }
            };
            orderResources.ForEach(o => context.OrderResources.Add(o));
            context.SaveChanges();

            // Advance
            var advances = new List<ADVANCE>
            {
                new ADVANCE { amount = 150, referenceNumber = "abc123", type = "Western Union" },
                new ADVANCE { amount = 250, referenceNumber = "def473", type = "Western Union" }
            };
            advances.ForEach(a => context.Advances.Add(a));
            context.SaveChanges();

            // Order Advance
            var orderAdvances = new List<ORDER_ADVANCE>
            {
                new ORDER_ADVANCE{ advanceID = advances[0].advanceID, orderID = orders[0].orderID },
                new ORDER_ADVANCE{ advanceID = advances[1].advanceID, orderID = orders[1].orderID }
            };
            orderAdvances.ForEach(oa => context.OrderAdvances.Add(oa));
            context.SaveChanges();

            // Alert
            var alerts = new List<ALERT>
            {
                new ALERT{ subject = "Test 1", content ="My extreemely new content.", date = DateTime.Now, read = false },
                new ALERT{ subject = "Test 2", content ="My extreemely old content.", date = DateTime.Now.AddDays(-2), read = false }
            };
            alerts.ForEach(a => context.Alerts.Add(a));
            context.SaveChanges();
        }
    }
}
